package com.paisheng.justlife.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hb.dialog.dialog.ConfirmDialog;
import com.paisheng.justlife.R;
import com.paisheng.justlife.adapter.GvMenuAdapter;
import com.paisheng.justlife.appliction.MyApplication;
import com.paisheng.justlife.bean.Waste;
import com.paisheng.justlife.common.MenuType;
import com.paisheng.justlife.common.WasteMenu;
import com.paisheng.justlife.service.SerialService;
import com.paisheng.justlife.utils.SerialPortUtils;

import java.util.List;

/**
 * 投递首页
 */
public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView metal, plastic, clothes, paper, harmful;
    private View imageLog;
    private Intent intent;
    private SerialPortUtils serialPortUtils;
    private TextView tv_test;
    private Button recycle, helpCenter;
    private ConfirmDialog confirmDialog;
    int COUNTS = 1;// 点击次数
    long DURATION = 3000;// 规定有效时间
    long[] mHits = new long[COUNTS];
    private GridView gv_menu;
    private List<Waste> list = WasteMenu.getMentList();
    private GvMenuAdapter adapter;
    String status;
    String phone;
    MyApplication context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        registerClick();
        context = (MyApplication) getApplicationContext();
        intent= new Intent(context, SerialService.class);
        context.startService(intent);

    }

    private void initView() {  //--------------------------初始化id
        imageLog = findViewById(R.id.image_log);
        recycle = findViewById(R.id.recycle);
        tv_test = findViewById(R.id.tv_test);
        helpCenter = findViewById(R.id.help_center);
        gv_menu = (GridView) findViewById(R.id.gv_menu);
        imageLog.setOnClickListener(this);
        recycle.setOnClickListener(this);
        helpCenter.setOnClickListener(this);
        adapter = new GvMenuAdapter(MainActivity.this, list, MenuType.DELIVER);
        gv_menu.setAdapter(adapter);

    }

    private void registerClick() {   //---------------------------注册点击事件
        gv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = list.get(i).getName();
                switch (name) {
                    case (MenuType.METAL):
                        sendIntent("openClass", "metal");
                        break;
                    case (MenuType.PLASTIC):
                        sendIntent("openClass", "plastic");
                        break;
                    case (MenuType.CLOTHES):
                        sendIntent("openClass", "clothes");
                        break;
                    case (MenuType.PAPER):
                        sendIntent("openClass", "paper");
                        break;
                    case (MenuType.HARMFUL):
//                        sendIntent("openClass", "harmful");
                        break;
                }
            }
        });

    }


    @Override
    public void onClick(View view) { //----------------------------点击事件
        switch (view.getId()) {
            case R.id.recycle: //上门回收
                confirmDialog = new ConfirmDialog(this);
                confirmDialog.setLogoImg(R.mipmap.dialog_notice).setMsg("上门回收电话：10010");
                confirmDialog.setClickListener(new ConfirmDialog.OnBtnClickListener() {
                    @Override
                    public void ok() {

                    }

                    @Override
                    public void cancel() {

                    }
                });
                confirmDialog.show();
                break;
            case R.id.help_center: //帮助中心
                confirmDialog = new ConfirmDialog(this);
                confirmDialog.setLogoImg(R.mipmap.dialog_notice).setMsg("客服电话：10086");
                confirmDialog.setClickListener(new ConfirmDialog.OnBtnClickListener() {
                    @Override
                    public void ok() {

                    }

                    @Override
                    public void cancel() {

                    }
                });
                confirmDialog.show();
                break;
            case R.id.image_log:
                continuousClick(COUNTS, DURATION);
                break;
            default:
        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        status = intent.getStringExtra("status");
        phone = intent.getStringExtra("phone");
    }

    private void sendIntent(String name, String value) { //----------------------封装跳转页面
        if (status == null || status.equals("Finish")) {
            intent = new Intent(MainActivity.this, VerifyActivity.class);
        } else if (status.equals("Continue")) {
            intent = new Intent(MainActivity.this, BinOpenActivity.class);
            intent.putExtra("phone",phone );
        }
        intent.putExtra(name, value);
        startActivity(intent);
    }


    private void continuousClick(int count, long time) {  //跳转到回收操作页面
        //每次点击时，数组向前移动一位
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        //为数组最后一位赋值
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        if (mHits[0] >= (SystemClock.uptimeMillis() - time)) {
            mHits = new long[count];//重新初始化数组
            intent = new Intent(MainActivity.this, CollectorActivity.class);
//            intent = new Intent(MainActivity.this, CloseBoorActivity.class);
//            intent = new Intent(MainActivity.this, ClaimGoodsActivity.class);
            startActivity(intent);
        }
    }


}
